﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;


namespace TestCenter.Classes
{
    class TestManager
    {
        public Test Test { get; set; }
        public int TimePassed { get; set; }
        public Question CurrentQuestion { get; set; }
        public Timer Timer { get; set; }
        public List<string>[] Events;

        public KeyValuePair<string, bool>[][] ResultTable;

        public int DoneQuestion
        {
            get { return Array.FindAll(Test.Questions, b => b.HasDone).Length; }
        }

        public event EventHandler TimerOnTick;
        public event EventHandler TimeOut;
        public event EventHandler ChangeQuestion;

        public TestManager(Test test)
        {
            Test = test;
            TimePassed = 0;
            Timer = new Timer { Interval = 1000, Enabled = false };
            Timer.Tick += TimerEvent;
            Events = new List<string>[test.TotalQuestion];
            for (var i = 0; i < Events.Length; ++i)
                Events[i] = new List<string>();

            Test.InitQuestions();
        }

        public void Start()
        {
            CurrentQuestion = Test.Questions[0];
            ChangeQuestion(this, null);

            Timer.Start();
        }

        public void Resume()
        {
            Timer.Start();
        }

        public void Pause()
        {
            Timer.Stop();
        }

        public void Stop()
        {
            Timer.Stop();
        }

        private void TimerEvent(object sender, EventArgs eventArgs)
        {
            if (TimePassed >= Test.TimeInSecond)
            {
                Timer.Stop();
                TimeOut(sender, eventArgs);
                return;
            }
            TimePassed += 1;
            TimerOnTick(sender, eventArgs);
        }

        public void NextQuestion()
        {
            var i = CurrentQuestion.Index;
            i = (i + 1) % Test.TotalQuestion;
            CurrentQuestion = Test.Questions[i];
            ChangeQuestion(this, null);
        }

        public void PreviousQuestion()
        {
            var i = CurrentQuestion.Index;
            i = (i + Test.TotalQuestion - 1) % Test.TotalQuestion;
            CurrentQuestion = Test.Questions[i];
            ChangeQuestion(this, null);
        }

        public void Update()
        {
            ChangeQuestion(this, null);
        }

        public void CalcResult()
        {
            var qs = Test.Questions;
            ResultTable = new KeyValuePair<string, bool>[qs.Length][];

            for (var i = 0; i < qs.Length; ++i)
            {
                ResultTable[i] = new KeyValuePair<string, bool>[qs[i].Answers.Length];
                int start = -1;
                for (var j = 0; j < qs[i].Answers.Length; ++j)
                {
                    var s = qs[i].Answers[j];
                    start += 1;
                    var c = IsCompleted(s, Events[i], ref start);
                    ResultTable[i][j] = new KeyValuePair<string, bool>(s.UniqueString, c);
                }
            }
        }

        private bool IsCompleted(ICheckAction action, List<string> events, ref int start)
        {
            if (action.Type == "Event")
            {
                while (start < events.Count)
                {
                    if (action.UniqueString.ToLower().Equals(events[start].ToLower()))
                        return true;
                    ++start;
                }
            }
            return false;
        }
    }
}
